package com.sl.biz.modular.workflow.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.biz.modular.process.controller.request.ProcessRelationConfigSaveRequest;
import com.sl.biz.modular.process.controller.request.SquareLakeProcessRemoveByIdRequest;
import com.sl.biz.modular.process.dao.domain.dto.ProcessVersionDTO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessConfigDO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessRelationConfigDO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessVersionDO;
import com.sl.biz.modular.process.service.ProcessConfigBizService;
import com.sl.biz.modular.process.service.ProcessRelationConfigBizService;
import com.sl.biz.modular.process.service.ProcessVersionBizService;
import com.sl.biz.modular.process.service.ProcessVersionContentBizService;
import com.sl.biz.modular.workflow.dao.domain.entity.WorkflowDO;
import com.sl.biz.modular.workflow.dao.service.WorkflowDaoService;
import com.sl.biz.modular.workflow.service.WorkflowBizService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class WorkflowBizServiceImpl implements WorkflowBizService {

    private final WorkflowDaoService workflowDaoService;

    private final ProcessConfigBizService processConfigBizService;

    private final ProcessRelationConfigBizService processRelationConfigBizService;

    private final ProcessVersionBizService processVersionBizService;

    private final ProcessVersionContentBizService contentBizService;


    @Override
    public List<WorkflowDO> list(WorkflowDO workflowDO) {
        if (ObjectUtil.equals(workflowDO.getGroupId(), "0")) {
            workflowDO.setGroupId(null);
        }
        return workflowDaoService.queryByCondition(workflowDO);
    }

    @Override
    public Boolean save(WorkflowDO workflowDO) {
        boolean save = workflowDaoService.save(workflowDO);
        //这里还要保存流程表
        List<ProcessRelationConfigDO> workflow = processRelationConfigBizService.queryByBizIdAndBizType(workflowDO.getId(), "WORKFLOW");
        String processId = null;
        String processVersion = "1.0.0";
        String processVersionId = null;
        String relationId = null;
        if (CollUtil.isNotEmpty(workflow)) {
            processId = CollUtil.get(workflow, 0).getProcessId();
            processVersion = CollUtil.get(workflow, 0).getProcessVersion();
            relationId = CollUtil.get(workflow, 0).getId();
        }
        if (StrUtil.isAllNotBlank(processId, processVersion)) {
            ProcessVersionDO processVersionDO = new ProcessVersionDO();
            processVersionDO.setProcessId(processId);
            processVersionDO.setProcessVersion(processVersion);
            ProcessVersionDTO byProcessIdAndVersion = processVersionBizService.getByProcessIdAndVersion(processVersionDO);
            processVersionId = byProcessIdAndVersion.getId();
        }
        ProcessConfigDO processConfigDO = new ProcessConfigDO();
        processConfigDO.setId(processId);
        processConfigDO.setProductId(workflowDO.getProductId());
        processConfigDO.setGroupId(workflowDO.getGroupId());
        processConfigDO.setProcessName(workflowDO.getWorkflowName());
        processConfigDO.setDescription(workflowDO.getWorkflowName());
        processConfigDO.setProcessCode(workflowDO.getWorkflowCode());
        processConfigDO.setWorkflowFlag("1");
        processConfigDO.setRowSetTag("db");
        processConfigDO.setRowSetTagDataSourceId("default");
        processConfigBizService.save(processConfigDO);

        //保存版本信息
        ProcessVersionDO processVersionDO = new ProcessVersionDO();
        processVersionDO.setId(processVersionId);
        processVersionDO.setProcessId(processConfigDO.getId());
        processVersionDO.setProcessVersion(processVersion);
        processVersionDO.setDescription(processConfigDO.getDescription());
        processVersionDO.setDisableFlag(0);
        processVersionDO.setLatestFlag(1);
        processVersionBizService.save(processVersionDO);

        //建立关联关系
        ProcessRelationConfigSaveRequest saveRequest = new ProcessRelationConfigSaveRequest();
        saveRequest.setBizId(workflowDO.getId());
        saveRequest.setId(relationId);
        saveRequest.setBizType("WORKFLOW");
        saveRequest.setProcessId(processConfigDO.getId());
        saveRequest.setProcessVersion(processVersion);
        processRelationConfigBizService.save(saveRequest);


        return save;
    }

    @Override
    public Boolean removeById(WorkflowDO workflowDO) {
        //删除content
//        workflowContentDaoService.removeByWorkflowId(workflowDO.getId());
        boolean b = workflowDaoService.removeById(workflowDO);

        List<ProcessRelationConfigDO> workflow = processRelationConfigBizService.queryByBizIdAndBizType(workflowDO.getId(), "WORKFLOW");

        if (CollUtil.isEmpty(workflow)) {
            return false;
        }

        for (ProcessRelationConfigDO processRelationConfigDO : workflow) {

            String processId = processRelationConfigDO.getProcessId();
            String processVersion = processRelationConfigDO.getProcessVersion();
            processVersionBizService.removeByProcessIdAndVersion(processId, processVersion);
            //删除流程
            SquareLakeProcessRemoveByIdRequest squareLakeProcessRemoveByIdRequest = new SquareLakeProcessRemoveByIdRequest();
            squareLakeProcessRemoveByIdRequest.setId(processRelationConfigDO.getProcessId());
            processConfigBizService.removeById(squareLakeProcessRemoveByIdRequest);

            processRelationConfigBizService.remoteById(processRelationConfigDO.getId());

        }

        return b;
    }
}
